﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using ITM495.Lab2.Core;
using ITM495.Lab2.Core.Entities;

namespace ITM495.Lab2.Impl.Readers
{
    public class XmlReader: IReader<Tweet>
    {
        protected String Path { get; set; }

        public XmlReader(string path)
        {
            Path = path;
        }

        #region IReader<Tweet> Members

        public IList<Tweet> Read()
        {
            var tweetDoc = XDocument.Load(Path);

            var tweets = (from d in tweetDoc.Descendants("item")
                         select new Tweet()
                                    {
                                        Description = d.Element("description").Value,
                                        Guid = d.Element("guid").Value,
                                        Link = d.Element("link").Value,
                                        PubDate = DateTime.Parse(d.Element("pubDate").Value),
                                        Title = d.Element("title").Value
                                    }).ToList();

            return tweets;
         }

        #endregion
    }
}
